package demo.jdk.features.Base;

import java.util.concurrent.locks.StampedLock;

/**
 * @author Ranchoz  2018/9/10 8:51
 **/
public class BankAccountWithStampedLock {

	private final StampedLock lock = new StampedLock();
	private double balance;

	/**
	 *
	 * @param amount
	 */
	public void deposit(double amount) {
		long stamp = lock.writeLock();
		try {
			balance = balance + amount;
		} finally {
			lock.unlockWrite(stamp);
		}
	}

	/**
	 *
	 * @return
	 */
	public double getBalance() {
		long stamp = lock.readLock();
		try {
			return balance;
		} finally {
			lock.unlockRead(stamp);
		}
	}
}
